package com.heima.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-08-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;

    @Autowired
    private MediaFeign mediaFeign; //有的同学这里有红色波浪线 这是假错误

    @Autowired
    private ArticleFeign articleFeign; //有的同学这里有红色波浪线 这是假错误

    @Override
    public ResponseResult listByStatus(AuthDto dto) {

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();

//        构建查询条件
        if(dto.getStatus()!=null){
            queryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }

//        构建分页
        Page<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());

        Page<ApUserRealname> page1 = this.page(page, queryWrapper);


        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords()); // Long currentPage, Integer size, Long total, T data
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) { // 1:通过  0：失败
        Integer id = dto.getId();


        LambdaQueryWrapper<ApUserRealname>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getUserId,dto.getId());
        ApUserRealname userRealname = this.getOne(queryWrapper);

        if(userRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(type==0){  //0：失败
//            修改状态 设置失败的原因
            userRealname.setStatus(2);
            userRealname.setReason(dto.getMsg());
            this.updateById(userRealname);
        }else{
            ApUser apUser = apUserService.getById(userRealname.getUserId());
            WmUser wmUser = createWmUser(apUser);
//            2、成为文章作者   远程调用article微服务 想ap_author表插入数据
            ApAuthor apAuthor = createApAuthor(apUser,wmUser);

//            给wmUser赋apAuthorId
            wmUser.setApAuthorId(apAuthor.getId());
            mediaFeign.updateWmUser(wmUser);
//           3、修改状态  ApUserRealname修改成9  还需要修改ap_user表
            userRealname.setStatus(9);
            this.updateById(userRealname);

            apUser.setFlag(1);  //0 普通用户            1 自媒体人            2 大V
            apUser.setIsIdentityAuthentication(true); //是否身份认证
            apUserService.updateById(apUser);

        }

        return ResponseResult.okResult();
    }

    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
//        apAuthor.setId(); //主键自增
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);  //0 爬取数据            1 签约合作商            2 平台自媒体人
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode()!=0) {
            throw new RuntimeException("远程调用失败");
        }else{
            return   responseResult.getData();
        }

    }

    private WmUser createWmUser(ApUser apUser) {
        //            1、成为自媒体人  远程调用media微服务 想wm_user表插入数据
        WmUser wmUser = new WmUser();
//            BeanUtils.copyProperties(apUser,wmUser);
//            wmUser.setId(); //主键自增的 不用处理
        wmUser.setApUserId(apUser.getId());
//            wmUser.setApAuthorId(); //文章作者id  一会儿再赋值
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
//            wmUser.setNickname(); //无法赋值
        wmUser.setImage(apUser.getImage());
//            wmUser.setLocation(apUser.get);   //无法赋值
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);  //状态            0 暂时不可用            1 永久不可用            9 正常可用
//            wmUser.setEmail();  //无法赋值
        wmUser.setType(0);  //账号类型            0 个人             1 企业            2 子账号
        wmUser.setScore(0);
//            wmUser.setLoginTime();   //无法赋值

//            远程调用
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode()!=0) {
            throw new RuntimeException("远程调用失败");
        }else{
            return responseResult.getData();
        }
    }
}
